﻿using System;
using System.ComponentModel;


namespace Marmat.Forms
{
    public partial class BindingField
    {
        public class OptionsConverter : ExpandableObjectConverter
        {
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                if (ReferenceEquals(destinationType, typeof(string)) && value is BindingField)
                {
                    BindingField Field = (BindingField)value;
                    string DataSource = null;
                    string DataMember = null;
                    if (Field.DataSource is object)
                    {
                        DataSource = ((Component)Field.DataSource).Site.Name;
                    }

                    if (!string.IsNullOrEmpty(Field.DataMember))
                    {
                        DataMember = Field.DataMember;
                    }

                    if (string.IsNullOrEmpty(DataSource) && string.IsNullOrEmpty(DataMember))
                    {
                        return "(无)";
                    }

                    return DataSource + "." + DataMember;
                }

                return destinationType.Name;
            }

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return base.GetPropertiesSupported(context);
            }
        }
    }
}